package com.jt.safe.ui.staff.fragment


import androidx.recyclerview.widget.LinearLayoutManager
import com.jt.safe.R
import com.jt.safe.base.BaseFragment
import com.jt.safe.entity.CalendarBean
import com.jt.safe.entity.DateBean
import com.jt.safe.ui.adpter.TasksOfNursingPostListAdapter
import com.jt.safe.ui.adpter.TasksOfNursingPostViewPagerAdapter
import com.jt.safe.utils.LunarDayUtil
import com.jt.safe.utils.SpecialDayUtil
import com.jt.safe.viewmodel.NullViewModel
import kotlinx.android.synthetic.main.fragment_tasks_of_nursing_post.*
import kotlinx.android.synthetic.main.home_title_bar_layout.*
import java.util.*

class TasksOfNursingPostFragment : BaseFragment<NullViewModel>() {
    val calendars = mutableListOf<CalendarBean>()
    val data = mutableListOf("", "")

    companion object {
        fun newInstance() = TasksOfNursingPostFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_tasks_of_nursing_post

    override fun createObserver() {

    }

    override fun initView() {
        title_tv.text = "护学岗任务"
        initDataDay()
        val tasksOfNursingPostViewPagerAdapter = TasksOfNursingPostViewPagerAdapter()
        view_pager.adapter = tasksOfNursingPostViewPagerAdapter
        tasksOfNursingPostViewPagerAdapter.setNewData(calendars)

        val tasksOfNursingPostListAdapter = TasksOfNursingPostListAdapter()
        recycler.layoutManager = LinearLayoutManager(activity)
        recycler.adapter = tasksOfNursingPostListAdapter
        tasksOfNursingPostListAdapter.setNewData(data)
    }

    override fun initData() {

    }

    private fun initDataDay() {
        val todayCalendar = Calendar.getInstance()
        todayCalendar.setTime(Date())
        var calendarBean: CalendarBean
        var groupIndex = 0
        for (index in 0..7) {
            calendarBean = CalendarBean()
            val calendarClone = todayCalendar.clone() as Calendar
            calendarClone.add(Calendar.MONTH, index)
            calendarClone.set(Calendar.DATE, 1)
            calendarBean.year = calendarClone.get(Calendar.YEAR);
            calendarBean.month = calendarClone.get(Calendar.MONTH) + 1;
            calendarBean.shownTitle = "${calendarBean.year}-${calendarBean.getMonth()}"
            // 1-星期天 7-星期六
            val dayOfWeek = calendarClone.get(Calendar.DAY_OF_WEEK);
            //上月的最后几天展示在本月
            val emptyCount = dayOfWeek - 1
            calendarClone.roll(Calendar.DATE, -1)
            // 当月的最大天数
            val maxDays = calendarClone.get(Calendar.DATE)
            val daysList = mutableListOf<DateBean>()
            var maxRows = 5
            if (emptyCount + maxDays > 35) {
                //当月有效日期+1号前空白日期个数>35,则当月需要6行
                maxRows = 6;
            }
            for (index in 0 until maxRows * 7) {
                val dayItem = DateBean()
                // 用于控制定位
                dayItem.groupIndex = groupIndex
                dayItem.childIndex = index

                val calendarDayClone = calendarClone.clone() as Calendar
                if (index < emptyCount) {
                    dayItem.setCanSelect(false)
                    //上月最后几天
                    calendarDayClone.add(Calendar.DATE, (index - emptyCount));
                    dayItem.shownDay = "";
                    dayItem.nongliDay = "";
                    daysList.add(dayItem);

                    continue;
                }
                if (index >= emptyCount + maxDays) {
                    dayItem.setCanSelect(false);
                    //下月头几天
                    calendarDayClone.add(Calendar.MONTH, 1);
                    calendarDayClone.set(Calendar.DATE, index - (emptyCount + maxDays) + 1);
                    dayItem.shownDay = ""
                    dayItem.nongliDay = ""
                    daysList.add(dayItem)
                    continue
                }
                calendarDayClone.set(Calendar.DATE, index - emptyCount + 1);
                //显示的日期
                dayItem.shownDay = calendarDayClone.get(Calendar.DATE).toString()
                //显示的农历
                dayItem.nongliDay = LunarDayUtil(calendarDayClone).toStringSimpleDay();
                //显示的特殊日子
                dayItem.specialDayTag =
                    SpecialDayUtil.getInstance().getHolidayName(calendarDayClone);

                dayItem.year = calendarDayClone.get(Calendar.YEAR);
                dayItem.month = calendarDayClone.get(Calendar.MONTH) + 1;
                dayItem.day = calendarDayClone.get(Calendar.DATE);
                dayItem.dayOfWeek = calendarDayClone.get(Calendar.DAY_OF_WEEK);
                dayItem.isGovHoliday = SpecialDayUtil.getInstance().isGovHoliday(calendarDayClone);
                dayItem.isGovHolidayWork =
                    SpecialDayUtil.getInstance().isGovHolidayWork(calendarDayClone);

                if (calendarDayClone.before(todayCalendar)) {
                    //今天之前
                    dayItem.isCanSelect = false
                } else if (calendarDayClone == todayCalendar) {
//                    dayItem.shownDay = "今天"
                    dayItem.isCanSelect = true
                    dayItem.isCheck = true
                } else {
                    //今天之后
                    dayItem.isCanSelect = true
                }
                dayItem.saverCalendar = calendarDayClone
                daysList.add(dayItem)
            }
            calendarBean.dateBeans = daysList
            calendars.add(calendarBean)
            groupIndex++
        }
    }
}
